package com.quicks.beauty.server.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.quicks.beauty.core.dao.BeautyProductMapper;
import com.quicks.beauty.core.dao.CustomMapper;
import com.quicks.beauty.core.dao.ProductPicsMapper;
import com.quicks.beauty.core.entity.ProductPics;
import com.quicks.beauty.core.entity.ProductPicsExample;
import com.quicks.beauty.core.vo.BeautyProductDetailVO;
import com.quicks.beauty.core.vo.BeautyProductVO;
import com.quicks.beauty.core.vo.ProdSubMinMax;
import com.quicks.beauty.core.vo.ProductSub;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class ProductService {

    @Autowired
    private BeautyProductMapper productMapper;

    @Autowired
    private ProductPicsMapper picsMapper;

    @Autowired
    private CustomMapper customMapper;

    /*
    分页获取产品数目
     */
    public List<BeautyProductVO> getAllBeautyProdPager(int pageNum, int pageSize) throws Exception {
        PageHelper.startPage(pageNum, pageSize, true);
        Page<BeautyProductVO> pager = productMapper.getAllBeautyProdPager();
        List<BeautyProductVO> result = pager.getResult();
        DecimalFormat df = new DecimalFormat("#");
        result.forEach(bpo -> {
            if (bpo.getKinds() == 2) {
                ProdSubMinMax price = customMapper.findProdSubMinMaxPrice(bpo.getId(), 0);
                BigDecimal minPrice = price.getMinPrice();
                BigDecimal maxPrice = price.getMaxPrice();

                if (minPrice.compareTo(maxPrice) == 0) {
                   // minPrice = minPrice.multiply(BigDecimal.TEN);
                    String min = df.format(minPrice);
                    bpo.setPriceStr(min);
                   // bpo.setPointStr(min);
                } else {
                 //   maxPrice = maxPrice.multiply(BigDecimal.TEN);
                  //  minPrice = minPrice.multiply(BigDecimal.TEN);
                    String min = df.format(minPrice);
                    String max = df.format(maxPrice);
                    bpo.setPriceStr(min + "—" + max);
                }
                if(price.getMinPoints().compareTo(price.getMaxPoints()) == 0){
                    bpo.setPointStr(price.getMinPoints().toString());
                }else{
                    bpo.setPointStr(price.getMinPoints().toString()+"—"+price.getMaxPoints().toString());
                }
            }
        });
        return result;
    }

    public BeautyProductDetailVO getProdItemDetailVo(String prodId) {
        BeautyProductDetailVO vo = productMapper.getProdItemDetailVo(prodId);

        Integer kinds = vo.getKinds();
        if (kinds == 2) {
            DecimalFormat df = new DecimalFormat("#");

            List<ProductSub> prodSubs = customMapper.findProdSubs(prodId, 0);
            List<BigDecimal> collect = prodSubs.stream().map(lo -> lo.getPrice()).collect(Collectors.toList());
            BigDecimal maxPrice = collect.stream().max(BigDecimal::compareTo).get();
            BigDecimal minPrice = collect.stream().min(BigDecimal::compareTo).get();

            if (minPrice.compareTo(maxPrice) == 0) {
                BigDecimal minPrice1 = minPrice;
                minPrice = minPrice.multiply(BigDecimal.TEN);
                String min = df.format(minPrice);
                vo.setPointStr(min);
                vo.setPriceStr(minPrice1.toString());
            } else {
                BigDecimal maxPrice1 = maxPrice;
                BigDecimal minPrice1 = minPrice;
                maxPrice = maxPrice.multiply(BigDecimal.TEN);
                minPrice = minPrice.multiply(BigDecimal.TEN);
                String min = df.format(minPrice);
                String max = df.format(maxPrice);
                vo.setPointStr(min + "—" + max);
                vo.setPriceStr(minPrice1.toString()+"—"+maxPrice1.toString());
            }
            vo.setProdSubs(prodSubs);
        }
        ProductPicsExample example = new ProductPicsExample();
        example.createCriteria().andDeletedEqualTo(0).andProdIdEqualTo(prodId);
        example.setOrderByClause("nums desc");
        List<ProductPics> list = picsMapper.selectByExample(example);
        if (!list.isEmpty()) {
            List<String> collect = list.stream().map(productPics -> productPics.getUrls()).collect(Collectors.toList());
            vo.setImageSet(collect);
        } else {
            List<String> strings = Arrays.asList(vo.getImages());
            vo.setImageSet(strings);
        }
        return vo;
    }
}
